import { defineConfig } from '@vben/vite-config';
import legacy from "@vitejs/plugin-legacy";
import { loadEnv } from 'vite';
const root = process.cwd();

import ElementPlus from 'unplugin-element-plus/vite';

export default defineConfig(async ({ command, mode }) => {
  const environment = loadEnv(mode,root);
  return {
    application: {},
    vite: {
      plugins: [
        ElementPlus({
          format: 'esm',
        }),
        {
          name: "jingzhi-script",
          transformIndexHtml(html) {
            return html.replace(
              "</title>",
              `</title>
      <script>if(!(/Chrome\\/(\\d+)/.test(navigator.userAgent)&&parseInt(RegExp.$1)<92)){window.__vite_is_modern_browser=true}</script>`);
          }
        },
        legacy({
          targets: "chrome < 92"
        })
      ],
      server: {
        proxy: {
          '/api-mock': {
            changeOrigin: true,
            rewrite: (path) => path.replace(/^\/api-mock/, ''),
            target: 'http://localhost:5320/api', // mock代理目标地址
            ws: true
          },
          [environment.VITE_API_BASE]:{
            target: environment.VITE_DEVELOPMENT_SERVER_ADDRESS,
            rewrite: (path) => path.replace(new RegExp("^" + environment.VITE_API_BASE), ""),
            changeOrigin: true,
            ws: true,
            secure: false,
            configure: (proxy, options) => {
              proxy.on("proxyReq", (proxyReq, req, res) => {
                console.log("\x1b[34m[vite代理请求]" + req.method + " " + req.url + " -> " + options.target + req.url + "\x1b[0m");
              });
            }
          }
        },
      },
    },
  };
});
